package com.deneme;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import functions.images.CommonHTML;
import functions.images.CommonHTML.PageName;

public class ViewPuzzles extends HttpServlet {
    private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    public void doGet(HttpServletRequest req,
			HttpServletResponse resp)
					throws IOException {
    	/* Get user information */
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		UserData userData = UserData.getDataForUser(user);
		/* Get puzzle keys for the logged in user */
		ArrayList<String> puzzleKeys = userData.getPuzzleKeys();
		
		/* for each puzzle key the user has, add it to the display */
		/* this code builds the string that hold the puzzle div tag */
		StringBuilder sb_table = new StringBuilder();
		sb_table.append("\n<div class='view_puzzles'>\n");
		if (puzzleKeys != null && puzzleKeys.size() > 0) {
			for (String puzzKey : puzzleKeys) {
				System.out.println("displaying puzzle: " + puzzKey);
				/* get the puzzle assocated with the key */
				SavedPuzzle puzz = SavedPuzzle.getPuzzleForKey(puzzKey);
				/* if the puzzle exists...*/
				if (puzz != null) {
					/* retrieve the image and display a thumbnail of it, along with
					 * other puzzle identifying information and statistics */
					sb_table.append("<div class='view_single_puzzle'>" +
						"	<a href='/puzzle?puzzle-key=" + puzzKey + "'><img height='200' width='200' src='/serve?blob-key=" + puzz.getImgKey().getKeyString() + "' /></a>" +
						"		<strong>Dimension</strong>: " + puzz.getDimension() + " x " + puzz.getDimension() + "<br />\n" +
						"		<strong>Difficulty</strong>: " + puzz.getDifficulty().toString() + "<br />\n" +
						"		<strong>Percent Complete</strong>: " + puzz.getPercentComplete() + "%<br />\n" +
						"		<strong>Saved Date</strong>: " + puzz.getSaveDate().toString() + "<br />\n" +
						"		<strong>Contributors</strong>: " + puzz.getContributorsString() + "<br />\n" +
						"		<br /><a class='fun_button' href='/deletepuzzle?key=" + puzzKey + "'>Delete</a><br />\n" +
						"</div><br /><!-- view_single_puzzle -->\n");
				}
			}
		}
		/* if the user has no saved puzzles, display this message instead */
		else {
			sb_table.append("You do not have any saved puzzles");
		}
		sb_table.append("</div><!-- view_puzzles -->\n");
		
		/* create the page view that will hold the list of saved puzzles */
		String html = 	"<html>\n" +
					  	"	<head>\n" +
						"		<link rel=\"stylesheet\" href=\"/styles.css\" type=\"text/css\" />\n" +
					  	"		<title>Your Pictures!</title>\n" +
					  	"	</head>\n" +
					  	"	<body>\n" +
					  	"		<div class='content'>" +
					  	CommonHTML.getNavBar(PageName.puzzles) + // this includes the menu at the top of the page
					  	"		<br />" + sb_table.toString() + "" +
					  	"		</div><!-- content -->" +
					  	"	</body>\n" +
					  	"</html>\n";
		
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.print(html);
    }
				
}
